// 给一个数组和一个目标元素，查看这个元素是否含有这个目标元素，如果有返回数组的下标
// 没有就返回-1
function search(nums, target) {
  let left = 0
  let right = nums.length - 1
  while (left <= right) {
    // 四舍五入
    let mid = Math.round((left + right) / 2)
    if (nums[mid] > target) right = mid - 1
    else if (nums[mid] < target) left = mid + 1
    else return mid
  }
  return -1
}
console.log(search([1, 2, 3, 4, 5], 3));